clear all

********************************************************************************
********************************************************************************
**** When Can We Trust Regression Discontinuity Design Estimates from Close ****
*************** Elections? Evidence from Experimental Benchmarks ***************
********************************************************************************
********************************************************************************

* This file produces Figure OA2 in Appendix B of De Magalhaes et al. (Political
* Analysis).

cd "$dir"
use "$dir/data/personal_incumbency_advantage.dta", clear

********************************************************************************

* RDD plots: Elected t + 1 

cd "$dir/temp/"	

rdbwselect electednextelection rv if rv!=0 & country=="Brazil", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot electednextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Brazil", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect electednextelection rv if rv!=0 & country=="Brazil", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Brazil", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Brazil", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Brazil", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Brazil", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Brazil", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Brazil", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Brazil", lcolor(blue) lwidth(thin) lpattern(--))
graph save "brazil_elected_rdplot.gph", replace
drop rdplot* n


rdbwselect electednextelection rv if rv!=0 & country=="Denmark", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot electednextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Denmark", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect electednextelection rv if rv!=0 & country=="Denmark", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Denmark", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Denmark", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Denmark", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Denmark", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Denmark", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit electednextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Denmark", lcolor(red) lwidth(medthick)) ///
	(qfit electednextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Denmark", lcolor(blue) lwidth(thin) lpattern(--))
graph save "Denmark_elected_rdplot.gph", replace
drop rdplot* n

* RDD plots: Running t + 1

rdbwselect runningnextelection rv if rv!=0 & country=="Brazil", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot runningnextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Brazil", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect runningnextelection rv if rv!=0 & country=="Brazil", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Brazil", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Brazil", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Brazil", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Brazil", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Brazil", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Brazil", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Brazil", lcolor(blue) lwidth(thin) lpattern(--))
graph save "brazil_rerun_rdplot.gph", replace
drop rdplot* n


rdbwselect runningnextelection rv if rv!=0 & country=="Denmark", all vce(cluster constituency) kernel(uniform) p(2)
local h_CER2=e(h_cerrd)
rdplot runningnextelection rv if abs(rv)<=`h_CER2' & rv!=0 & country=="Denmark", p(2) genvars binselect(es)
bysort rdplot_mean_y rdplot_mean_x: gen n = _n
replace rdplot_mean_y = . if n != 1
replace rdplot_mean_x = . if n != 1
rdbwselect runningnextelection rv if rv!=0 & country=="Denmark", all vce(cluster constituency) kernel(uniform)
local h_MSE1=e(h_mserd)
local h_CER1=e(h_cerrd)
twoway (scatter rdplot_mean_y rdplot_mean_x if abs(rv)<=`h_CER2' & rv!=0 & country=="Denmark", plotregion(lcolor(black)) ylabel(, format(%3.2f) angle(0)) xlabel(#5, format(%3.2f)) msize(small) mcolor(gs6) xline(0, lcolor(gs12)) xtitle("Vote margin") ytitle("Elected t+1") legend(off) graphregion(color(white))) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER1' & country=="Denmark", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv<0 & abs(rv)<=`h_MSE1' & country=="Denmark", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv<0 & abs(rv)<=`h_CER2' & country=="Denmark", lcolor(blue) lwidth(thin) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER1' & country=="Denmark", lcolor(red) lwidth(medthick) lpattern(--)) ///
	(lfit runningnextelection rv if rv>0 & abs(rv)<=`h_MSE1' & country=="Denmark", lcolor(red) lwidth(medthick)) ///
	(qfit runningnextelection rv if rv>0 & abs(rv)<=`h_CER2' & country=="Denmark", lcolor(blue) lwidth(thin) lpattern(--))
graph save "denmark_rerun_rdplot.gph", replace
drop rdplot* n

********************************************************************************

* Robustness to alternative bandwidths: Elected t + 1

cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect electednextelection rv if country=="Brazil" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect electednextelection rv if country=="Brazil" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust electednextelection rv if country=="Brazil" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_Brazil_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_Brazil_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_Brazil_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_Brazil_rdrobust`i'.dta
		}

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) xline(`h_MSE', lcolor(red) lpattern(solid)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xline(`h_MSE2', lcolor(blue) lwidth(thin) lpattern(solid)) ///
		xline(`h_CER2', lpattern(dash) lcolor(blue) lwidth(thin)) ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 
		
	graph save locallinear_Brazil_standard1.gph, replace

	sum bandwidth if parm=="Robust"
	local jump = 0.5
	local xmin = 0.5
	local xmax = r(max)	
	}

cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect electednextelection rv if country=="Denmark" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect electednextelection rv if country=="Denmark" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust electednextelection rv if country=="Denmark" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_Denmark_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_Denmark_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_Denmark_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_Denmark_rdrobust`i'.dta
		}

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) xline(`h_MSE', lcolor(red) lpattern(solid)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xline(`h_MSE2', lcolor(blue) lwidth(thin) lpattern(solid)) ///
		xline(`h_CER2', lpattern(dash) lcolor(blue) lwidth(thin)) ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 

		graph save locallinear_Denmark_standard1.gph, replace

	}

* Robustness to alternative bandwidths: Running t + 1

cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect runningnextelection rv if country=="Brazil" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect runningnextelection rv if country=="Brazil" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust runningnextelection rv if country=="Brazil" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_Brazil_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_Brazil_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_Brazil_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_Brazil_rdrobust`i'.dta
		}

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) xline(`h_MSE', lcolor(red) lpattern(solid)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xline(`h_MSE2', lcolor(blue) lwidth(thin) lpattern(solid)) ///
		xline(`h_CER2', lpattern(dash) lcolor(blue) lwidth(thin)) ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 

		graph save locallinear_Brazil_rerun_standard1.gph, replace

	sum bandwidth if parm=="Robust"
	local jump = 0.5
	local xmin = 0.5
	local xmax = r(max)	

	}

cd "$dir/data"
use "personal_incumbency_advantage.dta", clear

quietly {
	use "$dir/data/personal_incumbency_advantage.dta", clear
	
	rdbwselect runningnextelection rv if country=="Denmark" & rv!=0, all vce(cluster constituency) kernel(uniform)
	local h_MSE=e(h_mserd)
	local h_CER=e(h_cerrd)
	rdbwselect runningnextelection rv if country=="Denmark" & rv!=0, all vce(cluster constituency) kernel(uniform) p(2)
	local h_MSE2=e(h_mserd)
	local h_CER2=e(h_cerrd)
	
	cd "$dir/temp"
	
	forvalues c = 10(1)400 {
		local h = `c'/100
		rdrobust runningnextelection rv if country=="Denmark" & rv!=0, rho(1) all vce(cluster constituency) kernel(uniform) h(`h')
		parmest, saving(locallinear_Denmark_rdrobust`c'.dta, replace)
		}

	forvalues i = 10(1)400 {
		use locallinear_Denmark_rdrobust`i'.dta, clear
		gen bandwidth = `i'/100
		save, replace
		}

	use locallinear_Denmark_rdrobust10.dta, clear

	forvalues i = 11(1)400 {
		append using locallinear_Denmark_rdrobust`i'.dta
		}

	sum bandwidth if parm=="Conventional"
	local jump = 0.5
	local xmin = 0
	local xmax = r(max)

	twoway (scatter estimate bandwidth if parm=="Conventional", connect(direct) lwidth(medthick) lcolor(red) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Robust", fcolor(blue%30) lwidth(none)) ///
		(scatter estimate bandwidth if parm=="Robust", ///
		connect(direct) lwidth(medium) lcolor(blue) lwidth(thin) lpattern(--) ///
		msymbol(none)) (rarea min95 max95 bandwidth if parm=="Conventional", fcolor(red%30) lwidth(none) lpattern(solid) ///
		plotregion(lcolor(black)) xline(`h_MSE', lcolor(red) lpattern(solid)) ///
		xline(`h_CER', lpattern(dash) lcolor(red))  ///
		xline(`h_MSE2', lcolor(blue) lwidth(thin) lpattern(solid)) ///
		xline(`h_CER2', lpattern(dash) lcolor(blue) lwidth(thin)) ///
		xlabel(`xmin'(`jump')`xmax', format(%3.2f)) ylabel(, format(%3.2f) angle(0)) ///
		yline(0, lcolor(black) lwidth(vthin)) legend(off) xtitle("Bandwidth") ///
		ytitle(Estimated effect) graphregion(color(white))) 

		graph save locallinear_Denmark_rerun_standard1.gph, replace

	}

********************************************************************************

* Combine the graphs

graph combine "Brazil_rerun_rdplot.gph" "locallinear_Brazil_rerun_standard1.gph", graphregion(color(white)) cols(2) subtitle("Brazil", position(11))
graph save "Brazil_rerun.gph", replace
graph combine "Denmark_rerun_rdplot.gph" "locallinear_Denmark_rerun_standard1.gph", graphregion(color(white)) cols(2) subtitle("Denmark", position(11))
graph save "Denmark_rerun.gph", replace

graph combine "Brazil_rerun.gph" "Denmark_rerun.gph", graphregion(color(white)) cols(1) xsize(3) ysize(2) subtitle("Panel A: Running t+1", position(11))
graph export "$dir/figures/figure_OA2A.pdf", as(pdf) replace

graph combine "Brazil_elected_rdplot.gph" "locallinear_Brazil_standard1.gph", graphregion(color(white)) cols(2) subtitle("Brazil", position(11))
graph save "Brazil_elected.gph", replace
graph combine "Denmark_elected_rdplot.gph" "locallinear_Denmark_standard1.gph", graphregion(color(white)) cols(2) subtitle("Denmark", position(11))
graph save "Denmark_elected.gph", replace

graph combine "Brazil_elected.gph" "Denmark_elected.gph", graphregion(color(white)) cols(1) xsize(3) ysize(2) subtitle("Panel B: Elected t+1", position(11))
graph export "$dir/figures/figure_OA2B.pdf", as(pdf) replace

********************************************************************************
